/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-07-23 11:02:22
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2025-03-14 15:55:04
 * @FilePath: \dwys_web\extensions\phoenix-plugin-client\assets\core\platform\platform.base.ts
 * @Description: 
 */
import { ErrorLocalCode } from "../defines/error.local.define";

export class PlatformBase {
    protected urlBase:string = '';
    protected timeout:number = 10000;
    protected appid:string = '';

    setAppId(appid: string) {
        this.appid = appid;
    }

    setBaseUrl(url: string) {
        this.urlBase = url;
    }

    setRequestTimeOut(timeout: number) {
        this.timeout = timeout;
    }

    needThirdPlatformLogin(){
        return true;
    }

    public async request(method:string, url:string, data:any, token?:string): Promise<any> {
        return new Promise((resolve, reject) => {
            if (token) {
                let xhr = new XMLHttpRequest();
                xhr.timeout = this.timeout;
                const requetUrl:string = `${this.urlBase}${url}`;
                xhr.onerror = () => {
                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:{}});
                };
                xhr.ontimeout = () => {
                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:{}});
                };
                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200 || xhr.status === 201) {
                            if (xhr.responseText) {
                                try {
                                    resolve(JSON.parse(xhr.responseText));
                                } catch (error) {
                                    resolve({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:xhr.responseText});
                                }
                            }
                        } else {
                            if (xhr.responseText) {
                                try {
                                    let msg = JSON.parse(xhr.responseText);
                                    reject({ code: msg.code ?? ErrorLocalCode.REQUEST_FAILED, message: msg });
                                } catch (error) {
                                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:xhr.responseText});
                                }
                            }                 
                        }
                    }
                };
                xhr.open(method, requetUrl, true);
                xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
                xhr.setRequestHeader('Authorization', `Bearer ${token}`);
                xhr.send(JSON.stringify(data));
            } else {
                let xhr = new XMLHttpRequest();
                xhr.timeout = this.timeout;
                const requetUrl:string = `${this.urlBase}${url}`;
                xhr.onerror = () => {
                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:{}});
                };
                xhr.ontimeout = () => {
                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:{}});
                };
                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200 || xhr.status === 201) {
                            if (xhr.responseText) {
                                try {
                                    resolve(JSON.parse(xhr.responseText));
                                } catch (error) {
                                    resolve({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:xhr.responseText});
                                }
                            }
                        } else {
                            if (xhr.responseText) {
                                try {
                                    let msg = JSON.parse(xhr.responseText);
                                    reject({ code: msg.code ?? ErrorLocalCode.REQUEST_FAILED, message: msg });
                                } catch (error) {
                                    reject({code: ErrorLocalCode.REQUEST_TIMEOUT, message: '请求超时,或返回数据异常', data:xhr.responseText});
                                }
                            }
                        }
                    }
                };
                xhr.open(method, requetUrl, true);
                xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
                xhr.send(JSON.stringify(data));
            }
        });
    }
}